/* Copyright © Worklight Ltd. 2006-2011.  All rights reserved. */

package com.worklight.wlclient.api;

import org.json.JSONException;
import org.json.JSONObject;

import com.worklight.wlclient.WLClientUtils;

/**
 * This class hold the data return from the response
 */
public class WLProcedureInvocationResult extends WLResponse {

	private static final String JSON_KEY_IS_SUCCESSFUL = "isSuccessful";
	private JSONObject jsonResult;	
	
	/**
	 * Create new WLProcedureInvocationResult from JSONObject
	 * @param jsonResult - JSONObject represent the JSON response text
	 */	
	public WLProcedureInvocationResult(WLResponse response) {
		super(response);		 		
	}

	public boolean isSuccessful() {
		boolean isSuccessful = false;
		try {
			isSuccessful = getResult().getBoolean(JSON_KEY_IS_SUCCESSFUL);
		} catch (JSONException e) {
			//DO Noithing here
		}
		return isSuccessful;
	}
	
	public JSONObject getResult() throws JSONException {
		if (jsonResult == null){
			jsonResult = WLClientUtils.convertStringToJSON(responseText);
		}
		return jsonResult;
	}

	@Override
	public String toString() {
		return "WLProcedureInvocationResult [isSuccessful=" + isSuccessful() + 
			", result=" + jsonResult + "]";
	}
}
